<?php

/**
*Form Builder & Admin Generator

*@author marcio <opi14@op.pl>, <polishvodka7@gmail.com>
*@copyright Copyright (c) 2011, marcio
*@version 1.6.5
*/

class Volta_Form {

	/**
	*Skladowa klasy ktora przechowywuje kod html formularza
	*@static
	*@access protected
	*@var string $form
	*/
	protected static $form = '';
	
	/**
	*Skladowa klasy ktora przechowywuje formularz jako osobne elementy tablicy
	*@static
	*@access protected
	*@var array $array_form
	*/
	protected static $array_form = array();
	
	/**
	*Skladowa klasy gdy true formularz jest tworzony za pomoca tabel
	*@static
	*@access protected
	*@var bool $table
	*/
	protected static $table = false;
	
	/**
	*Skladowa klasy ktora ustawia wyglada formularzy
	*@static
	*@access protected
	*@var string $layout
	*/
	protected static $layout = 'standard';

		
	/**
	*Tworzy formularz
	*@static
	*@access public 
	*@param string $action odslylanie do akcji formylarza
	*@param string $method metoda wysylania formularza get/post
	*@param bool $enctype gdy true formularz dla plikow
	*/
	public static function open($action = '', $method = 'post', $enctype = null) 
	{	
		//if(empty($action))
			//$action = $_SERVER['PHP_SELF'];
	
		if($enctype !== null)
		{
			self::$form .= '<form method="'.$method.'" action="'.$_SERVER['PHP_SELF'].$action.'" enctype="multipart/form-data">';
			self::$array_form['form_open'] .= htmlspecialchars('<form method="'.$method.'" action="'.$_SERVER['PHP_SELF'].$action.'" enctype="multipart/form-data">');
		}
			
		else
		{
			self::$form .= '<form method="'.$method.'" action="'.$_SERVER['PHP_SELF'].$action.'">';
			self::$array_form['form_open'] .= htmlspecialchars('<form method="'.$method.'" action="'.$_SERVER['PHP_SELF'].$action.'">');
		}
	}
	
	/**
	*Tworzy formularz w tabeli
	*@see Volta_Form::open()
	*/
	public static function open_with_table($action = '', $method = 'post', $enctype = null) 
	{
		self::set_table();
		self::open($action, $method, $enctype);
		self::$form .= '<table align="center" style="background-color:#ffffff;border: 1px solid black">';	
	}
	
	protected static function layout($name)
	{
		if(is_array($name)) 
			$name = $name['name'];

		if(self::$layout == 'standard' && self::$table === true)
			$layout = '<div><b>'.ucfirst($name).':</b></div>';
		else if(self::$layout == 'side' && self::$table === true)
			$layout = '<b>'.ucfirst($name).':</b></td><td>';
			
		return $layout;
	}
		
	/**
	*Tworzy html dla input text
	*@static
	*@access public 
	*@param string $name atrybut name input text'a
	*@param string $value atrybut value input text'a
	*@param string $css css dla input text'a
	*/
	public static function text($name, $value = null, $css = null) 
	{	
	    self::open_rows_and_cols();
		self::$form .= self::layout($name);
		
		if(empty($css)) 
			$def_css = 'height:30px;width:200px;padding:2px;background-color:#ffffff;border:2px solid #B8860B;';
			
		else 
			$def_css = $css;
	
		if(is_string($name))
		{
			self::$form .= '<input type="text" name="'.$name.'" value="'.$value.'" style="'.$def_css.'">';
			self::$array_form[$name] = htmlspecialchars('<input type="text" name="'.$name.'" value="'.$value.'" style="'.$def_css.'">');
		}
		
		else
		{
			self::$form .= '<input type="text" ';
			self::$array_form[$name['name']] .= htmlspecialchars('<input type="text" ');
		
			if(!in_array('style', $name))
				self::$form .= 'style="'.$def_css.'"';
		
			foreach($name as $tag => $value)
			{		
				self::$form .= $tag . '="' .$value. '" ';
				self::$array_form[$name['name']] .= htmlspecialchars($tag . '="' .$value. '" ');
			}
			
			self::$form .= '>';
			self::$array_form[$name['name']] .= '>';
		}

		self::close_rows_and_cols();
	}
	
	/**
	*Tworzy html dla input password
	*@static
	*@access public 
	*@param string $name atrybut name input pwd
	*@param string $value atrybut value input pwd
	*@param string $css css dla input pwd
	*/
	public static function password($name, $value = null, $css = null) 
	{
	
		self::open_rows_and_cols();
		self::$form .= self::layout($name);
			
		if(empty($css)) 
			$def_css = 'height:30px;width:200px;padding:2px;background-color:#ffffff;border:2px solid #B8860B;';
			
		else 
			$def_css = $css;
			
		if(is_string($name))
		{
			self::$form .= '<input type="password" name="'.$name.'" value="'.$value.'" style="'.$def_css.'">';
			self::$array_form[$name] = htmlspecialchars('<input type="password" name="'.$name.'" value="'.$value.'" style="'.$def_css.'">');
		}
		
		else
		{
			self::$form .= '<input type="password" ';
			self::$array_form[$name['name']] .= htmlspecialchars('<input type="password" ');
		
			if(!in_array('style', $name))
				self::$form .= 'style="'.$def_css.'"';
		
			foreach($name as $tag => $value)
			{		
				self::$form .= $tag . '="' .$value. '" ';
				self::$array_form[$name['name']] .= htmlspecialchars($tag . '="' .$value. '" ');
			}
			
			self::$form .= '>';
			self::$array_form[$name['name']] .= '>';
		}
		
		self::close_rows_and_cols();
	}
	
	/**
	*Tworzy html dla button'a
	*@static
	*@access public 
	*@param string $name atrybut name dla button'a
	*@param string $value atrybut value dla button'a
	*@param string $css css dla button'a
	*/
	public static function button($name, $value = null, $css = null) 
	{
		self::open_rows_and_cols();
		self::$form .= self::layout($name);
		
		if(empty($css)) 
			$def_css = 'background-color:#ffffff;border:1px solid black;';
			
		else 
			$def_css = $css;
			
		if(is_string($name))
		{
			self::$form .= '<input type="button" name="'.$name.'" value="'.$value.'" style="'.$def_css.'">';
			self::$array_form[$name] = htmlspecialchars('<input type="button" name="'.$name.'" value="'.$value.'" style="'.$def_css.'">');
		}
		
		else
		{
			self::$form .= '<input type="button" ';
			self::$array_form[$name['name']] .= htmlspecialchars('<input type="button" ');
		
			if(!in_array('style', $name))
				self::$form .= 'style="'.$def_css.'"';
		
			foreach($name as $tag => $value)
			{		
				self::$form .= $tag . '="' .$value. '" ';
				self::$array_form[$name['name']] .= htmlspecialchars($tag . '="' .$value. '" ');
			}
			
			self::$form .= '>';
			self::$array_form[$name['name']] .= '>';
		}
		
		self::close_rows_and_cols();
	}
	
	/**
	*Tworzy html dla textarea
	*@static
	*@access public 
	*@param string $name atrybut name dla textarea
	*@param string $value atrybut value dla textarea
	*@param string $css css dla textarea
	*/
	public static function textarea($name, $value = null, $css = null) 
	{
	
		self::open_rows_and_cols();
		self::$form .= self::layout($name);
		
		if(empty($css)) 
			$def_css = 'height:100px;width:300px;padding:2px;background-color:#ffffff;border:2px solid #B8860B;';
			
		else 
			$def_css = $css;
			
		if(is_string($name))
		{
			self::$form .= '<textarea name="'.$name.'" style="'.$def_css.'">'.$value.'</textarea>';
			self::$array_form[$name] = htmlspecialchars('<textarea name="'.$name.'" style="'.$def_css.'">'.$value.'</textarea>');
		}
        
		else
		{
			self::$form .= '<textarea ';
			self::$array_form[$name['name']] .= htmlspecialchars('<textarea ');
			
			if(!in_array('style', $name))
				self::$form .= 'style="'.$def_css.'"';
			
			foreach($name as $tag => $value)
			{
				if($tag != 'value')
				{
					self::$form .= $tag . '="' .$value. '" ';
					self::$array_form[$name['name']] .= htmlspecialchars($tag . '="' .$value. '" ');
				}
				
				else if($tag == 'value')
				{
					self::$form .= '>'.$value;
					self::$array_form[$name['name']] .= htmlspecialchars('>'.$value);
				}
			}
			
			self::$array_form[$name['name']] .= htmlspecialchars('</textarea>');
			self::$form .= '</textarea>';
		}
		
		self::close_rows_and_cols();	
	}
	
	/**
	*Tworzy html dla input file
	*@static
	*@access public 
	*@param string $name atrybut name input file'a
	*@param string $css css dla input file'a
	*/
	public static function input_file($name, $css = null) 
	{
		self::open_rows_and_cols();
		self::$form .= self::layout($name);
		self::$form .= '<input type="file" name="'.$name.'" style="'.$css.'">';
		self::$array_form[$name] .= htmlspecialchars('<input type="file" name="'.$name.'" style="'.$css.'">');
		self::close_rows_and_cols();
	}
	
	/**
	*Tworzy kod html dla input hidden
	*@static
	*@access public 
	*@param string $name atrybut name dla input hidden
	*@param string $css css dla input hidden
	*/
	public static function hidden($name, $value = null) 
	{
		self::open_rows_and_cols();
			
		if(is_string($name))
		{
			self::$form .= '<input type="hidden" name="'.$name.'" value="'.$value.'">';
			self::$array_form[$name] = htmlspecialchars('<input type="hidden" name="'.$name.'" value="'.$value.'">');
		}
		
		else
		{
			self::$form .= '<input type="hidden" ';
			self::$array_form[$name['name']] .= htmlspecialchars('<input type="hidden" ');
		
			foreach($name as $tag => $value)
			{		
				self::$form .= $tag . '="' .$value. '" ';
				self::$array_form[$name['name']] .= htmlspecialchars($tag . '="' .$value. '" ');
			}
			
			self::$form .= '>';
			self::$array_form[$name['name']] .= '>';
		}
		
		self::close_rows_and_cols();
	}
	
	/**
	*Tworz kod html dla checkbox
	*@static
	*@access public 
	*@param string $name atrybut name input text'a
	*@param string $value atrybut value input text'a
	*@param bool $checked czy checkbox ma byc zaznaczony
	*@param string $css css dla input text'a
	*/
	public static function checkbox($name, $value = null, $checked = false, $css = null) 
	{
	
		self::open_rows_and_cols();
		self::$form .= self::layout($name);
		$check = ($checked === false) ? '' : 'checked="checked"';
		
		if(is_array($value)) 
		{
			foreach($value as $key => $data) 
			{
				self::$form .= '<input type="checkbox" name="'.$name.'" value="'.$key.'" '.$check.' style="'.$css.'">'.$data;
				self::$array_form[$name.'_'.$key] = htmlspecialchars('<input type="checkbox" name="'.$name.'" value="'.$key.'" '.$check.' style="'.$css.'">'.$data);
			}
			
			foreach($value as $k => $v)
			{
				self::$array_form[$name] .= self::$array_form[$name.'_'.$k];
				unset(self::$array_form[$name.'_'.$k]);
			}
		}
		
		else if($value === null)
		{
			self::$form .= '<input type="checkbox" name="'.$name.'" value="'.$name.'" '.$check.' style="'.$css.'">'.$name;
			self::$array_form[$name] .= htmlspecialchars('<input type="checkbox" name="'.$name.'" value="'.$name.'" '.$check.' style="'.$css.'">'.$name);
		}
		
		else
		{
			self::$form .= '<input type="checkbox" name="'.$name.'" value="'.$value.'" '.$check.' style="'.$css.'">'.$value;
			self::$array_form[$name] .= htmlspecialchars('<input type="checkbox" name="'.$name.'" value="'.$value.'" '.$check.' style="'.$css.'">'.$value);
		}
		
		self::close_rows_and_cols();
	}
	
	/**
	*@static
	*@access public 
	*@param string $name atrybut name dla radio
	*@param string $value atrybut value dla radio
	*@param bool $checked czy radio ma byc zaznaczony
	*@param string $css css dla radio
	*/
	//zrobic zeby metoda zwracala checlbox w jednej zmiennej jak menu
	public static function radio($name, $value = null, $checked = false, $css = null) 
	{
	
		self::open_rows_and_cols();
		self::$form .= self::layout($name);
		
		$check = ($checked === false) ? '' : 'checked="checked"';
		
		if(is_array($value)) 
		{
			foreach($value as $key => $data) 
			{
				self::$form .= '<input type="radio" name="'.$name.'" value="'.$key.'" '.$check.' style="'.$css.'">'.$data;
				self::$array_form[$name.'_'.$key] = htmlspecialchars('<input type="radio" name="'.$name.'" value="'.$key.'" '.$check.' style="'.$css.'">'.$data);
			}
			
			foreach($value as $k => $v)
			{
				self::$array_form[$name] .= self::$array_form[$name.'_'.$k];
				unset(self::$array_form[$name.'_'.$k]);
			}
		}
		
		else if($value === null)
		{
			self::$form .= '<input type="radio" name="'.$name.'" value="'.$name.'" '.$check.' style="'.$css.'">'.$name;
			self::$array_form[$name] .= htmlspecialchars('<input type="radio" name="'.$name.'" value="'.$name.'" '.$check.' style="'.$css.'">'.$name);
		}
		
		else
		{
			self::$form .= '<input type="radio" name="'.$name.'" value="'.$value.'" '.$check.' style="'.$css.'">'.$value;
			self::$array_form[$name] .= htmlspecialchars('<input type="radio" name="'.$name.'" value="'.$value.'" '.$check.' style="'.$css.'">'.$value);
		}
		
		self::close_rows_and_cols();
	
	}
	
	/**
	*@static
	*@access public 
	*@param string $name atrybut name dla menu
	*@param string $value atrybut value dla menu
	*@param bool $multiple czy menu jest multiple
	*@param string $css css dla menu
	*/
	public static function menu($name, $value, $multiple = false, $css = null) 
	{
	
		self::open_rows_and_cols();
		self::$form .= self::layout($name);
		
		$multi = ($multiple === false) ? '' : 'multiple="multiple"';
		
		self::$form .= '<select name="'.$name.'" '.$multi.'>';
		self::$array_form[$name.'_open'] = htmlspecialchars('<select name="'.$name.'" '.$multi.'>');
		
		if(is_array($value)) 
		{	
			foreach($value as $key => $option)
			{
				self::$form .= '<option value="'.$key.'">'.$option.'</option>';
				self::$array_form[$name.'_'.$key] = htmlspecialchars('<option value="'.$key.'">'.$option.'</option>');
			}
		}
		
		else
		{
			self::$form .= '<option value="'.$value.'">'.$value.'</option>';
			self::$array_form[$name] .= htmlspecialchars('<option value="'.$value.'">'.$value.'</option>');
		}
		
		self::$form .= '</select>';
		self::$array_form[$name.'_close'] .= htmlspecialchars('</select>');
		
		self::$array_form[$name] = self::$array_form[$name.'_open'];
		unset(self::$array_form[$name.'_open']);
		
		foreach($value as $key => $value)
		{
			self::$array_form[$name] .= self::$array_form[$name.'_'.$key];
			unset(self::$array_form[$name.'_'.$key]);
		}
		
		self::$array_form[$name] .= self::$array_form[$name.'_close'];
		unset(self::$array_form[$name.'_close']);
		
		self::close_rows_and_cols();
	
	}
	
	/**
	*Tworzy kod html dla input submit
	*@static
	*@access public 
	*@param string $name atrybut name input sumbit
	*@param string $value atrybut value input submit
	*@param string $css css dla input submit
	*/
	public static function submit($name = 'form_sbt', $value = 'submit', $css = null) 
	{
	
	    self::open_rows_and_cols('right');
		
		if(empty($css)) 
			$def_css = 'height:30px;width:100px;padding:2px;background-color:#FFF8DC;border:1px solid #A9A9A9;';
			
		else 
			$def_css = $css;
			
		if(self::$layout == 'side')
			self::$form .= '</td><td align="center">';
		
		if(is_string($name))
		{
			self::$form .= '<input type="submit" name="submit_'.$name.'" value="'.$value.'" style="'.$def_css.'">';
			self::$array_form[$name] = htmlspecialchars('<input type="submit" name="submit_'.$name.'" value="'.$value.'" style="'.$def_css.'">');
		}
		
		else
		{
			self::$form .= '<input type="submit" ';
			self::$array_form[$name['name']] .= htmlspecialchars('<input type="submit" ');
		
			if(!in_array('style', $name))
				self::$form .= 'style="'.$def_css.'"';
		
			foreach($name as $tag => $value)
			{		
				self::$form .= $tag . '="' .$value. '" ';
				self::$array_form[$name['name']] .= htmlspecialchars($tag . '="' .$value. '" ');
			}
			
			self::$form .= '>';
			self::$array_form[$name['name']] .= '>';
		}
		
		self::close_rows_and_cols();
	
	}
	
	/**
	*Ustawia typ layout'u
	*@static
	*@access public 
	*/
	public static function set_layout($layout) 
	{
		self::$layout = $layout;
	}
	
	/**
	*Jesli skladowa $property === true otwiera komorki tabeli
	*@static
	*@access public 
	*/
	public static function open_rows_and_cols($align = null) 
	{
		if(self::get_table())
		{
			if($align != null)
				self::$form .= '<tr><td align="'.$align.'">';
			else
				self::$form .= '<tr><td>';
		}
	}
	
	/**
	*Jesli skladowa $property === true zamyka komorki tabeli
	*@static
	*@access public 
	*/
	public static function close_rows_and_cols() 
	{
		if(self::get_table())
			self::$form .= '</td></tr>';
	}
	
	/**
	*Zamyka formularz
	*@static
	*@access public 
	*/
	public static function close() 
	{	
		self::$form .= '</form>';
		self::$array_form['form_close'] .= htmlspecialchars('</form>');
	}
	
	/**
	*Jesli skladowa $property === true zamyka tabele
	*@static
	*@access public 
	*/
	public static function close_with_table() 
	{
		self::$form .= '</table>';
		self::close();
	}
	
	/**
	*Dodaje kod html do formualrza
	*@static
	*@access public 
	*@param string $code
	*/
	public static function add_html_to_form($code)
	{
		self::open_rows_and_cols();
		self::$form .= $code;
		self::close_rows_and_cols();
	}
	
	/**
	*Zwraca formularz
	*@static
	*@access public 
	*@return string self::$form kod html formularza
	*/
	public static function get_form()
	{
		if(self::$table)
			return self::$form;	
			
		else
		{
			foreach(self::$array_form as $key => $value)
				self::$array_form[$key] = $value;
			return self::$array_form;
		}
	}
	
	/**
	*Zwraca czy formularz w tabeli jest wlaczony/wylaczony
	*@static
	*@access protected
	*@return bool $table
	*/
	protected static function get_table() 
	{
		return self::$table;
	}
	
	/**
	*Ustawia $table na true
	*@static
	*@access protected 
	*/
	public static function set_table($switch = false)
	{
		if(!$table)
			self::$table = true;
	}

}

?>